package com.neu.controller;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neu.po.ResponseResult;
import com.neu.po.User;
import com.neu.service.UserService;

@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("login")
	public ResponseResult login(String username,String password,String checkCode,HttpSession session) {
		ResponseResult result = new ResponseResult();
		
		String code = (String)session.getAttribute("code");
		
		if(!(code != null && code.equals(checkCode))){
			result.setResult("error");
			result.setData("验证码输入错误");
			return result;
		}
		User user = userService.login(username, password);
		
		if(user == null) {
			result.setResult("error");
			result.setData("用户名或密码输入错误");
		}else {
			result.setData(user);
		}
		return result;
	}
	
	@RequestMapping("checkCode")
	public boolean checkCode(String checkCode,HttpSession session) {
		String code = (String)session.getAttribute("code");
		if(!(code != null && code.equals(checkCode))){
			return false;
		}else {
			return true;
		}
	}
	
	@RequestMapping("getCode")
	public ResponseEntity<byte[]> getCode(HttpSession session) throws IOException {
		char[] arr = {'0','1','2','3','4','5','6','7','8','9'};
		
		StringBuilder stb = new StringBuilder();
		int n ;
		for(int i = 0 ; i < 4 ; i++) {
			n = (int)(Math.random()*arr.length);
			stb.append(arr[n]);
		}
		session.setAttribute("code", stb.toString());
		
		//把字符串写入到图片中
		//创建图片，设置图片大小及类型
		BufferedImage buffImg = new BufferedImage(90, 20, BufferedImage.TYPE_INT_RGB);
		//得到画布
		Graphics g = buffImg.getGraphics();
		//把字符串写入到画布中
		g.drawString(stb.toString(), 20, 15);
		//创建了一个字节数组输出流
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		//把图片写入到流内
		ImageIO.write(buffImg, "jpeg", bos);
		
		return new ResponseEntity<byte[]>(bos.toByteArray(),HttpStatus.CREATED);
	}
	
	@RequestMapping("insert")
	public ResponseResult insert(@RequestBody User user) {
		ResponseResult result = new ResponseResult();
		
		result.setData(userService.insert(user));
		
		return result;
	}
	
	@RequestMapping("update")
	public ResponseResult update(@RequestBody User user) {
		ResponseResult result = new ResponseResult();
		
		result.setData(userService.update(user));
		
		return result;
	}
	
	@RequestMapping("getPaged")
	public ResponseResult getPaged(int pageNum, int pageSize, String keyword) {
		ResponseResult result = new ResponseResult();
		
		result.setData(userService.getPaged(pageNum, pageSize, keyword));
		
		return result;
	}
	
	@RequestMapping("delete")
	public ResponseResult delete(int id) {
		ResponseResult result = new ResponseResult();
		
		result.setData(userService.delete(id));
		
		return result;
	}
}
